THash
CSharpTest.Net
HashDerivedBytes<THash> Class
Members  Example  See Also  Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Crypto Namespace : HashDerivedBytes<THash> Class

Glossary Item Box

Provided an implementation very similiar to that of the Rfc2898DeriveBytes with the following excpetions: 1) any hash size can be used, 2) original key bytes are always hashed, 3) bytes generated are always rounded to hash size, thus GetBytes(4) + GetBytes(4) != GetBytes(8)

Syntax

Visual Basic (Declaration) 
Public Class HashDerivedBytes(Of THash As {New, HMAC}) 
   Inherits System.Security.Cryptography.DeriveBytes
   Implements IPasswordDerivedBytes 
C# 
public class HashDerivedBytes<THash> : System.Security.Cryptography.DeriveBytes, IPasswordDerivedBytes  
where THash: new(), HMAC

Type Parameters

THash

Example

Library/Library.Test/TestPassword.cs

C#Copy Code
int iter = 100;
Salt salt = new Salt();
byte[] passbytes = Password.Encoding.GetBytes(TEST_PASSWORD);
IPasswordDerivedBytes[] types = new IPasswordDerivedBytes[] {
    new PBKDF2(passbytes, salt, iter),
    new HashDerivedBytes<HMACMD5>(passbytes, salt, iter),
    new HashDerivedBytes<HMACSHA1>(passbytes, salt, iter),
    new HashDerivedBytes<HMACSHA256>(passbytes, salt, iter),
    new HashDerivedBytes<HMACSHA384>(passbytes, salt, iter),
    new HashDerivedBytes<HMACSHA512>(passbytes, salt, iter),
};

foreach (IPasswordDerivedBytes db in types)
{
    byte[] key;
    
    Stopwatch w = new Stopwatch();
    w.Start();
    for (int i = 0; i < 100; i++)
        key = new PasswordKey(db, salt).CreateKey().Key;
    w.Stop();

    Console.Error.WriteLine("{0,10}  {1}", w.ElapsedMilliseconds, db.GetType().Name);
}
VB.NETCopy Code
Dim iter As Integer = 100
Dim salt As New Salt()
Dim passbytes As Byte() = Password.Encoding.GetBytes(TEST_PASSWORD)
Dim types As IPasswordDerivedBytes() = New IPasswordDerivedBytes() {New PBKDF2(passbytes, salt, iter), New HashDerivedBytes(Of HMACMD5)(passbytes, salt, iter), New HashDerivedBytes(Of HMACSHA1)(passbytes, salt, iter), New HashDerivedBytes(Of HMACSHA256)(passbytes, salt, iter), New HashDerivedBytes(Of HMACSHA384)(passbytes, salt, iter), New HashDerivedBytes(Of HMACSHA512)(passbytes, salt, iter)}

For Each db As IPasswordDerivedBytes In types
    Dim key As Byte()

    Dim w As New Stopwatch()
    w.Start()
    Dim i As Integer = 0
    While i < 100
        key = New PasswordKey(db, salt).CreateKey().Key
        System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
    End While
    w.[Stop]()

    Console.[Error].WriteLine("{0,10}  {1}", w.ElapsedMilliseconds, db.[GetType]().Name)
Next

Inheritance Hierarchy

System.Object
   System.Security.Cryptography.DeriveBytes
      CSharpTest.Net.Crypto.HashDerivedBytes<THash>

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys